package com.killer.sqlSession;

import com.killer.config.XmlConfigBuilder;
import com.killer.pojo.Configuration;
import org.dom4j.DocumentException;

import java.io.InputStream;

/**
 * 该类用于构建SqlSessionFactory对象
 */
public class SqlSessionFactoryBuilder {

    private Configuration configuration;

    /**
     * 解析配置文件并生成SqlSessionFactory对象
     * @param inputStream
     * @return
     */
    public SqlSessionFactory build(InputStream inputStream) throws DocumentException, ClassNotFoundException {
        XmlConfigBuilder xmlConfigBuilder = new XmlConfigBuilder();
        //解析xml文件，并获取configuration对象
        configuration = xmlConfigBuilder.parseConfiguration(inputStream);
        //同样,由于构造方法需要configuration对象，这里也是需要传入的
        //因此在这个方法之前需要解析配置文件，生成configuration对象，并传入sqlSessionFactory的构造方法中
        SqlSessionFactory sqlSessionFactory=new DefaultSqlSessionFactory(configuration);
        return sqlSessionFactory;
    }
}
